package com.hamizhong.iflowers.web.action.admin.prodgoods;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.IColorService;
import com.hamizhong.iflowers.service.IProductService;
import com.hamizhong.iflowers.service.IVarietyService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProductVO;

public class AjaxQueryProductAction extends MyBaseAjaxAction {

	private IProductService iProductService = null;
	private IVarietyService iVarietyService = null;
	private IColorService iColorService = null;
	
	public IProductService getiProductService() {
		return iProductService;
	}
	public void setiProductService(IProductService iProductService) {
		this.iProductService = iProductService;
	}
	public IVarietyService getiVarietyService() {
		return iVarietyService;
	}
	public void setiVarietyService(IVarietyService iVarietyService) {
		this.iVarietyService = iVarietyService;
	}
	public IColorService getiColorService() {
		return iColorService;
	}
	public void setiColorService(IColorService iColorService) {
		this.iColorService = iColorService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryProductAction...");
		String pageNum = getRQ().getParameter("pageNum");
		IOPResultDto<IObjectVO> resultDto = iProductService.findAll(Integer.valueOf(pageNum), MyConstants.PAGE_SIZE_COMMON_ADMIN);
		IObjectVO iObjectVO = resultDto.getFirstResult();
		List<ProductVO> productVOList = iObjectVO.getProductVOList();
		int totalProductCnt = iObjectVO.getTotalProductCnt();
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		//设置总条数
		jo.put("totalCnt", totalProductCnt+"");
		//设置当前页
		jo.put("pageNum", pageNum);
		//设置总页数
		jo.put("totalPageCnt", MyTools.convertItemCnt2PageCnt(totalProductCnt, MyConstants.PAGE_SIZE_COMMON_ADMIN)+"");
		for (ProductVO productVO : productVOList) {
			jobj = new JSONObject();
			jobj.put("productId", productVO.getProductId());
			jobj.put("productName", productVO.getProductName());
			jobj.put("varietyId", productVO.getVarietyVO().getVarietyId());
			jobj.put("varietyName", productVO.getVarietyVO().getVarietyName());
			jobj.put("colorId", productVO.getColorVO().getColorId());
			jobj.put("colorName", productVO.getColorVO().getColorName());
			jobj.put("sortId", productVO.getSortId());
			jobj.put("remarks", productVO.getRemarks());
			ja.add(jobj);
		}
		jo.put("data", ja.toString());
		jo.put("result", MyConstants.AJAX_SUCCESS);
		return jo;
	}

}
